// MiniView.cpp : implementation file
//

#include "stdafx.h"
#include "DrawDotMatrix.h"
#include "MiniView.h"
#include "DrawDotMatrixDoc.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CMiniView

IMPLEMENT_DYNCREATE(CMiniView, CScrollView)

CMiniView::CMiniView()
{
	// TODO: add construction code here
	m_pImg = NULL;

	DWORD bitmapInfoSize,i;

	// Bitmap info structure for the image
	bitmapInfoSize = sizeof(BITMAPINFO) + 255*sizeof(RGBQUAD);
	m_pBmiImage = (BITMAPINFO*)new BYTE[bitmapInfoSize];

	m_pBmiImage->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
	m_pBmiImage->bmiHeader.biPlanes = 1;
	m_pBmiImage->bmiHeader.biBitCount = 8;
	m_pBmiImage->bmiHeader.biCompression = BI_RGB;
	m_pBmiImage->bmiHeader.biSizeImage = 0;
	m_pBmiImage->bmiHeader.biXPelsPerMeter = 0;
	m_pBmiImage->bmiHeader.biYPelsPerMeter = 0;
	m_pBmiImage->bmiHeader.biClrUsed = 0;
	m_pBmiImage->bmiHeader.biClrImportant = 0;

	m_pBmiImage->bmiHeader.biWidth = 0;
	m_pBmiImage->bmiHeader.biHeight = 0;

	for (i = 0 ; i < 256 ; i++) {
		m_pBmiImage->bmiColors[i].rgbBlue = (BYTE)i;
		m_pBmiImage->bmiColors[i].rgbGreen = (BYTE)i;
		m_pBmiImage->bmiColors[i].rgbRed = (BYTE)i;
		m_pBmiImage->bmiColors[i].rgbReserved = 0;
	}
}

CMiniView::~CMiniView()
{
}


BEGIN_MESSAGE_MAP(CMiniView, CScrollView)
	//{{AFX_MSG_MAP(CMiniView)
		// NOTE - the ClassWizard will add and remove mapping macros here.
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMiniView drawing

void CMiniView::OnInitialUpdate()
{
	CDrawDotMatrixDoc* pDoc = (CDrawDotMatrixDoc* )GetDocument();
	CScrollView::OnInitialUpdate();

	CSize sizeTotal;
	// TODO: calculate the total size of this view
	sizeTotal.cx = pDoc->m_nWidth;
	sizeTotal.cy = pDoc->m_nHeight;
	SetScrollSizes(MM_TEXT, sizeTotal);
}

void CMiniView::OnDraw(CDC* pDC)
{
	CDrawDotMatrixDoc* pDoc = (CDrawDotMatrixDoc* )GetDocument();
	ASSERT_VALID(pDoc);

	if (m_pImg == NULL)
		return;

	// Set bitmap header info
	m_pBmiImage->bmiHeader.biWidth = pDoc->m_nWidth;
	m_pBmiImage->bmiHeader.biHeight = -pDoc->m_nHeight;

	// Display
	SetDIBitsToDevice(pDC->GetSafeHdc(),
					  0, 0, pDoc->m_nWidth, pDoc->m_nHeight,
					  0, 0, 0, pDoc->m_nHeight,
					  m_pImg, m_pBmiImage, DIB_RGB_COLORS);
	pDC->TextOut(10, 10, m_szText);
}

/////////////////////////////////////////////////////////////////////////////
// CMiniView diagnostics

#ifdef _DEBUG
void CMiniView::AssertValid() const
{
	CScrollView::AssertValid();
}

void CMiniView::Dump(CDumpContext& dc) const
{
	CScrollView::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CMiniView message handlers
void CMiniView::SetScrollingWindow(int dx, int dy)
{
	CDrawDotMatrixDoc* pDoc = (CDrawDotMatrixDoc*)GetDocument();
	CSize sizeTotal;

	// TODO: calculate the total size of this view
	sizeTotal.cx = dx;
	sizeTotal.cy = dy;
	SetScrollSizes(MM_TEXT, sizeTotal);

	Invalidate();
}
